package utils.shortcutKey;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tulskiy.keymaster.common.HotKey;
import com.tulskiy.keymaster.common.HotKeyListener;
import com.tulskiy.keymaster.common.Provider;
import javafx.application.Platform;
import javafx.geometry.Rectangle2D;
import javafx.stage.Stage;
import sample.CantSee;
import sample.MouseListener;
import utils.fileHandler.FileHandler;
import utils.jsonHandler.JsonReadWrite;

import javax.swing.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ShortcutKey {
    private static String root ;
    private static String inforoot ;
    private static Provider provider;
    private static Map<String,String> key_object;
    private static JSONObject shortcutKeyJson;
    public static JSONArray shortcutKeyList;
    private static boolean signal = false;
    static {
        root= FileHandler.getRootPath()+"json/";
        inforoot= FileHandler.getRootPath()+"source/";
    }
    public static void loadShortcutKey(){
        String path = root + "shortcutKeyRecord.json";
        provider = Provider.getCurrentProvider(false);
        String jsonRecord = JsonReadWrite.readJsonFile(path);
        shortcutKeyJson = JSON.parseObject(jsonRecord);
        shortcutKeyList = shortcutKeyJson.getJSONArray("shortcutKey");
        for(int i = 0;i < shortcutKeyList.size();i ++){
            JSONObject shortcutKey = (JSONObject)shortcutKeyList.get(i);
            String object = String.valueOf(shortcutKey.get("object"));
            String key = String.valueOf(shortcutKey.get("key"));
            registerKey(object,key);
        }

    }

    public static void addNewShortcutKey(String object,String key){
        String path = root + "shortcutKeyRecord.json";
        for(int i = 0;i < shortcutKeyList.size();i ++){
            JSONObject jsonobject = (JSONObject)shortcutKeyList.get(i);
            if(String.valueOf(jsonobject.get("object")).compareTo(object) == 0 || String.valueOf(jsonobject.get("key")).compareTo(key) == 0 ){
                shortcutKeyList.remove(i);
            }
        }
        Map<String,String> newKey = new HashMap<String,String>();
        newKey.put("object",object);
        newKey.put("key",key);
        shortcutKeyList.add(newKey);
        JsonReadWrite.writeJsonFile(path,shortcutKeyJson.toString());
        shutdownProvider();
        loadShortcutKey();
//        shortcutKeyJson.put();
    }

    public static void shutdownProvider(){
        provider.reset();
        provider.stop();
    }

    public static void deleteShortcutKey(String object){
        String path = root + "shortcutKeyRecord.json";
        for(int i = 0;i < shortcutKeyList.size();i ++){
            JSONObject jsonobject = (JSONObject)shortcutKeyList.get(i);
            if(String.valueOf(jsonobject.get("object")).compareTo(object) == 0 || String.valueOf(jsonobject.get("object")).compareTo("un" + object) == 0 ){
                shortcutKeyList.remove(i);
                JsonReadWrite.writeJsonFile(path,shortcutKeyJson.toString());
            }
        }
        shutdownProvider();
        loadShortcutKey();
    }

    public static void lockShortcutKey(){
        signal = true;
    }

    public static void releaseShortoutKey(){
        signal =false;
    }

    private static void registerKey(String object,String key){
        String title = object.split("_")[0];
        switch (title){
            case "spiltScrean":
                provider.register(KeyStroke.getKeyStroke(key), new HotKeyListener() {
                    @Override
                    public void onHotKey(HotKey hotKey) {
                        if(signal){
                            return;
                        }
                        if(CantSee.getChooseWindow() == null){
                            return;
                        }
                        Platform.runLater(new Runnable() {
                            @Override
                            public void run() {

                                Rectangle2D rectangle = CantSee.getRectangle();
                                Stage stage = CantSee.getStage();
                                try {
                                File file = new File(inforoot + object + ".txt");
                                BufferedReader br = new BufferedReader(new FileReader(file));
                                String str;
                                String[] strs = null;
                                CantSee.adapteScreen();
                                while (true) {
                                    if (!((str = br.readLine()) != null)) break;
                                    strs = str.split("#");
                                    String[] finalStrs = strs;
                                CantSee.addFrame(Double.valueOf(finalStrs[0])*4*CantSee.getScreenRectangle().getWidth()/1920, Double.valueOf(finalStrs[1])*4*CantSee.getScreenRectangle().getHeight()/1080,
                                        Double.valueOf(finalStrs[2])*4*CantSee.getScreenRectangle().getWidth()/1920, Double.valueOf(finalStrs[3])*4*CantSee.getScreenRectangle().getHeight()/1080);
                                }
                                    CantSee.show();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                        lockShortcutKey();
                    }
                });
                break;
            case "unspiltScrean":
                provider.register(KeyStroke.getKeyStroke(key), new HotKeyListener() {
                    @Override
                    public void onHotKey(HotKey hotKey) {
                        Platform.runLater(new Runnable() {
                            @Override
                            public void run() {
                                MouseListener.frameRectangle = new double[4];
                                CantSee.hideFrame();
                                releaseShortoutKey();
                            }
                        });

                    }
                });
                break;
            default:
                break;
        }
    }
}
